﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CoolChat
{
    public class ChatMessage
    {
        private int id = -1;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private ChatUser from;

        public ChatUser From
        {
            get { return from; }
            set { from = value; }
        }
        private ChatUser to;

        public ChatUser To
        {
            get { return to; }
            set { to = value; }
        }
        private string keyword = KEYWORD.ZH_CN.NONE;

        public string Keyword
        {
            get { return keyword; }
            set { keyword = value; }
        }
        private MsgType type;

        public MsgType Type
        {
            get { return type; }
            set { type = value; }
        }
        private string content="";

        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        private DateTime addtime = DateTime.Parse("2013-01-01 00:00:00");

        public DateTime Addtime
        {
            get { return addtime; }
            set
            {
                if (value == DateTime.MinValue)
                {
                    addtime = DateTime.Parse("2013-01-01 00:00:00");
                }
                else
                {
                    addtime = value;
                }
            }
        }

        private string msgID;

        public string MsgID
        {
            get { return msgID; }
            set { msgID = value; }
        }

        private DateTime msgTime = DateTime.Parse("2013-01-01 00:00:00");

        public DateTime MsgTime
        {
            get { return msgTime; }
            set
            {
                if (value == DateTime.MinValue)
                {
                    msgTime = DateTime.Parse("2013-01-01 00:00:00");
                }
                else
                {
                    msgTime = value;
                }
            }
        }

        public ChatMessage()
        {
        }
        public ChatMessage(int id,ChatUser from, ChatUser to ,string keyword, MsgType type,string content, DateTime addtime,string msgID, DateTime msgTime)
        {
            this.id = id;
            this.from = from;
            this.to = to;
            this.keyword = keyword;
            this.type = type;
            this.content = content;
            this.addtime = addtime;
            this.msgID = msgID;
            this.msgTime = msgTime;
        }

        public static ChatMessage GetMessage(System.Data.DataRow row)
        {
            int id = Convert.ToInt32(row["ID"]);
            ChatUser from = new ChatUser();
            from.UserID = Convert.ToString(row["FromID"]);
            from.UserIP = Convert.ToString(row["FromUserIP"]);
            from.UserName = Convert.ToString(row["FromUserName"]);
            ChatUser to = new ChatUser();
            to.UserID = Convert.ToString(row["ToID"]);
            to.UserIP = Convert.ToString(row["ToUserIP"]);
            to.UserName = Convert.ToString(row["ToUserName"]);
            string keyword = Convert.ToString(row["KeyWord"]);
            MsgType type = (MsgType)Convert.ToInt32(row["MsgType"]);
            if (type == MsgType.SYSUSERMSG)
            {
                from.UserRole = USER_ROLE.SYSUSER;
            }
            object utype = row["FromUserType"];
            from.SysUserType = utype == null || utype == DBNull.Value ? -1 : (int)utype;
            utype = row["ToUserType"];
            to.SysUserType = utype == null || utype == DBNull.Value ? -1 : (int)utype;
            string content = Convert.ToString(row["Content"]);
            DateTime addtime = Convert.ToDateTime(row["AddTime"]);
            string msgID = Convert.ToString(row["MsgID"]);
            DateTime msgTime = Convert.ToDateTime(row["MsgTime"]);
            ChatMessage msg = new ChatMessage(id, from, to, keyword, type, content, addtime, msgID, msgTime);
            return msg;
        }

    }
}